﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FYSJcrm.Core.Common
{
    public class DESEncrypt
    {
        private static string theKey = "!@#$%^&*";

        public static string Key
        {
            set { theKey = value; }
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="source">明文</param>
        /// <returns></returns>
        public static string Encrypt(string source)
        {
            return Encrypt(source, theKey);
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="source">明文</param>
        /// <param name="key">密钥，必须是8位或者16位字符</param>
        /// <returns></returns>
        public static string Encrypt(string source, string key)
        {
            if (source == null || source == "") return "";

            theKey = key;
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] input = Encoding.Default.GetBytes(source);
            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(input, 0, input.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }

            return ret.ToString();
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source">密文</param>
        /// <returns></returns>
        public static string Decrypt(string source)
        {
            return Decrypt(source, theKey);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="source">密文</param>
        /// <param name="key">密钥，必须是8位或者16位字符</param>
        /// <returns></returns>
        public static string Decrypt(string source, string key)
        {
            if (source == null || source == "") return "";

            theKey = key;
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[source.Length / 2];
            for (int x = 0; x < source.Length / 2; x++)
            {
                int i = (Convert.ToInt32(source.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();

            return Encoding.Default.GetString(ms.ToArray());
        }
    }
}
